package com.vhall.component.framework.common.utils;

import java.util.Objects;

/**
 * debug日志输出
 *
 * @author xiaoliang.wang@vhall.com
 */
public class DebugLogger {

    private final static DebugCollector HOLDER = new DebugCollector();

    final String fromClass;

    public DebugLogger(Class<?> fromClass) {
        this.fromClass = fromClass.getSimpleName();
    }

    /**
     * 获取 DebugLogger
     *
     * @param fromClass from class
     * @return debug logger
     */
    public static DebugLogger getLogger(Class<?> fromClass) {
        return new DebugLogger(fromClass);
    }

    /**
     * 从 thread local 中获取 DebugContext
     *
     * @return debug context
     */
    public static Object getDebugContext() {
        return HOLDER.get().toObject();
    }

    /**
     * 输出带 message 的 debug 信息
     *
     * @param message debug msg
     * @param debug   logger
     */
    public DebugLogger debug(String message, Object debug) {
        HOLDER.get().append(fromClass, message, debug);
        return this;
    }

    /**
     * 开启 debug 调试
     */
    public static void debugActive() {
        if (!isDebugActive()) {
            HOLDER.active();
        }
    }

    /**
     * 是否开启 debug 模式
     *
     * @return is debug enabled
     */
    public boolean isDebugEnabled() {
        return Objects.nonNull(HOLDER.get());
    }

    /**
     * 静态函数
     *
     * @return is active
     */
    public static boolean isDebugActive() {
        return Objects.nonNull(HOLDER.get());
    }

    /**
     * 清理 ThreadLocal
     */
    public static void remove() {
        HOLDER.remove();
    }

}
